$(function() {
    function split( val ) {
      return val.split(/,\s*/);
    }
    function extractLast( term ) {
      return split( term ).pop();
    }
 
    $( "#importance" )
      .bind( 'focus', function(){ $(this).autocomplete("search"); } ).bind( 'click', function(){ $(this).autocomplete("search"); } )
      .autocomplete({
        minLength: 0,
        source: function(request, response ) {
                $.ajax({
                    url: "/client/importance_josn",
                    dataType: "json",
                    data:{
						query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                        return {
						label: item.value,
						value: item.value
                        }
                        }));
                    }
                });
	    },
        focus: function() {
          return false;
        },
        select: function( event, ui ) {
          
		  if(this.value=="")
			this.value =ui.item.value;
		  else{
			  var terms = split( this.value );
		  terms.push( ui.item.value );
             this.value = terms.join( ", " );
		  }
          return false;
        }
      });
	  

	  $( "#nationality" )
      .bind( 'focus', function(){ 

      	$(this).autocomplete("search"); 

      }).bind( 'click', function(){ 

      	$(this).autocomplete("search"); 

      }).autocomplete({
        minLength: 0,
        source: function(request, response ) {
                $.ajax({
                    url: "/client/nationality_josn",
                    dataType: "json",
                    data:{
						query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                        return {
							label: item.value,
							value: item.value
                        }
                        }));
                    }
                });
	    }
	
		,
        focus: function() {
          return false;
        },
        select: function( event, ui ) {
          if(this.value=="")
			 this.value =ui.item.value;
		  else{
			 var terms = split( this.value );
		     terms.push( ui.item.value );
             this.value = terms.join( ", " );
		  }
          return false;
        }
    
      });

	  $( "#customer_source" )
      .bind( 'focus', function(){ $(this).autocomplete("search"); } ).bind( 'click', function(){ $(this).autocomplete("search"); } )
      .autocomplete({
        minLength: 0,
        source: function(request, response ) {
                $.ajax({
                    url: "/client/customer_source_josn",
                    dataType: "json",
                    data:{
						query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                        return {
						label: item.value,
						value: item.value
                        }
                        }));
                    }
                });
	    },
        focus: function() {
          return false;
        },
        select: function( event, ui ) {
         if(this.value=="")
			 this.value =ui.item.value;
		  else{
			 var terms = split( this.value );
		     terms.push( ui.item.value );
             this.value = terms.join( ", " );
		  }
          return false;
        }
      });


      
$( "#building_name" )
      .bind( 'focus', function(){ $(this).autocomplete("search"); } ).bind( 'click', function(){ $(this).autocomplete("search"); } )
      .autocomplete({
        minLength: 0,
        source: function(request, response ) {
                $.ajax({
                    url: "/client/building_name_josn",
                    dataType: "json",
                    data:{
						query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                        return {
						label: item.value,
						value: item.value
                        }
                        }));
                    }
                });
	    },
        focus: function() {
          return false;
        }
      });
	  $( "#category" )
      .bind( 'focus', function(){ $(this).autocomplete("search"); } ).bind( 'click', function(){ $(this).autocomplete("search"); } )
      .autocomplete({
        minLength: 0,
        source: function(request, response ) {
                $.ajax({
                    url: "/client/category_josn",
                    dataType: "json",
                    data:{
						query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                        return {
						label: item.value,
						value: item.value
                        }
                        }));
                    }
                });
	    },
        focus: function() {
          return false;
        },
        select: function( event, ui ) {
          if(this.value=="")
			 this.value =ui.item.value;
		  else{
			 var terms = split( this.value );
		     terms.push( ui.item.value );
             this.value = terms.join( ", " );
		  }
          return false;
        }
      });

	  $( "#name" ).autocomplete({
            source: function(request, response ) {
                $.ajax({
                    url: "/client/client_josn",
                    dataType: "json",
                    data:{
						query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
	                        return {
								label: item.name+"   "+item.recommended,
								value: item.name,
								recommended: item.recommended
	                        }
                        }));
                    }
                });
            },
			select: function(e, ui) {
			  $("#recommended").val(ui.item.recommended);
			},
            minLength: 1
 	});

	$( "#company_name" ).autocomplete({
            source: function(request, response ) {
                $.ajax({
                    url: "/client/company_name_josn",
                    dataType: "json",
                    data:{
						query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
	                        return {
								label: item.company_name,
								value: item.company_name								
	                        }
                        }));
                    }
                });
            },
            minLength: 1
 	});

	$( "#broker" ).autocomplete({
            source: function(request, response ) {
                $.ajax({
                    url: "/client/users_josn",
                    dataType: "json",
                    data:{
						query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
	                        return {
								label: item.name,
								value: item.name
	                        }
                        }));
                    }
                });
            },
            minLength: 1
 	});	
 });

